package com.travel.api.controller.order

import com.lgy.sell.controller.BaseController
import com.travel.api.beans.order.Order
import com.travel.api.beans.response.order.UserOrderInfoResponse
import com.travel.api.service.order.OrderService
import com.travel.api.service.order.OrderTypeService
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.util.CollectionUtils
import org.springframework.util.StringUtils
import org.springframework.web.bind.annotation.*

@Api(value = "/order/order", description = "订单")
@RestController
@RequestMapping("order/order")
@CrossOrigin
class OrderController: BaseController() {

    @Autowired
    lateinit var orderTypeService: OrderTypeService

    @Autowired
    lateinit var orderService: OrderService

    @ApiOperation(value = "用户订单列表", httpMethod = "GET", notes = "获取不同种类用户的订单列表")
    @GetMapping("/userOrderInfoList")
    fun userOrderInfoList(): Map<String, Any>? {
        // 根据token获取用户信息
        if(StringUtils.isEmpty(userInfo())) {
            return fail("没有该用户信息")
        }

        // TODO 这里前期这样简单点设计, 缺点当数据量一多会变慢
        // TODO 这里优化一下思路 获取一段时间内容的数据, 之前的数据当做旧数据 且设计订单的有效时间
        // TODO 对id字段加索引
        val userId = userInfo()!!.id
        val list = ArrayList<UserOrderInfoResponse>()
        orderTypeService.orderTypeList().forEach { orderType ->
            val response = UserOrderInfoResponse()
            // 通过订单类型Id和用户id去查询该类型的订单数量
            val order = Order()
            order.orderType = orderType.orderTypeId
            order.userId = userId
            val orderList = orderService.orderList(order)
            response.orderTypeId = orderType.orderTypeId
            response.icon = orderType.icon
            response.name = orderType.name

            if(CollectionUtils.isEmpty(orderList)) {
                response.number = 0
            } else {
                // 筛选出订单没有删除没有查看且订单没有完成的信息
                orderList.filter {
                    it.delFlag == "0"
                        &&
                    it.lookFlag == "0"
                        &&
                    it.orderFinish == "0"
                }
                response.number = orderList.size
            }
            list.add(response)
        }
        log.info("[用户订单列表]: {}", list)
        return success(null, list)
    }

    @ApiOperation(value = "获取指定类别的订单列表", httpMethod = "GET", notes = "获取指定类别的订单列表")
    @GetMapping("/orderInfoList")
    fun orderInfoList(@RequestParam orderTypeId: String): Map<String, Any>? {
        // 根据token获取用户信息
        if(StringUtils.isEmpty(userInfo())) {
            return fail("没有该用户信息")
        }

        // 通过用户的ID订单的种类ID查询订单信息列表
        var order = Order()
        order.userId = userInfo()!!.id
        order.orderType = orderTypeId
        val orderList = orderService.orderList(order)
        if(CollectionUtils.isEmpty(orderList)) {
            return success(null, null)
        }
        // 筛选出订单没有删除且订单没有完成的信息
        orderList.filter {
            it.delFlag == "0"
                &&
            it.orderFinish == "0"
        }
        log.info("[获取指定类别的订单列表]: {}", orderList)
        return success(null, orderList)
    }


}